<?php
//User interface that allows a user to complete a newly created inspection or finish an inspection in progress.
	include 'templateHandler.php';
	include 'commonAccountFunctions.php';
	
	//session_start();
	$user = $_SESSION['user'];
	
	if(userExists($_SESSION['user']) && hashMatch($_SESSION['user'])){
		// do nothing; page displays normally
	} else {
		// redirects to index because of wrong username/password
		header('Location: index.php');
	}
	
	print_header();
	
	$user = $_SESSION['user'];
	//$prevPage = $_SESSION['prev'];
	if(isset($_REQUEST["file"])){
		$file=trim($_REQUEST["file"]);
		$split=explode("_",$file);
		$facility= $split[0] . '_';
		$time= $split[1] . '_';
		$date= $split[2] . '_';
		$protocol= trim($split[3]);
		if(isset($split[4])){
			$suffix = "_" . trim($split[4]);
		}
		else{
			$suffix = "";
		}
	}
	else{
		$facility=$_REQUEST["facility"];
		$time= date("g-iA") . "_";
		$date = date("m-d-y") . "_";
		$protocol=trim($_REQUEST["protocol"]);
		$suffix = "";
	}
	?>
	<div id="topbar">
		<div id="title"> Lab-Check</div>
		<div id="rightbutton"> <a href="index.php" >Log Out</a> </div>
		<div id="leftbutton"> <a href="controlpanel.php" >Back</a> </div>
	</div>
	
	<div id="content">
		<ul class="pageitem" style="padding:5px">
			<li> <strong>User name:</strong>  <?=$user?> </li>
			<li><strong>File name:</strong> "<?= $facility, $time, $date, $protocol, $suffix?>"</li>
		</ul>
		
	<div class="forms">
		<!-- add function here that parses template file -->
		
		<?php 
			if(isset($_REQUEST["file"])){
				$arrayOfStrings = readIncomplete($file . ".txt");
				$parsedTemplate = parseTemplateLines($arrayOfStrings);
				$fileInfo = $parsedTemplate["fileInfo"];
				//print_r($fileInfo);
				if(isset($_REQUEST["notCom"])){
					echo "<h2 style=\"color:red\">";
					print($_REQUEST["notCom"]);
					echo "</h2>";
				}
				loadTemplate($parsedTemplate["groups"], $parsedTemplate["fileInfo"]);
			}
			else{
				$arrayOfStrings = readTemplate($protocol . ".txt");
				$parsedTemplate = parseTemplateLines($arrayOfStrings);
				loadTemplate($parsedTemplate["groups"], $parsedTemplate["fileInfo"]);
			}
	?></div><?php
	print_footer();
?>